package com.sneakxy.mybase4j.test;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.web.WebAppConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 测试用例基类
 * @author 潜行的青衣
 * @see
 */
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:spring/spring.xml"}/*classes = {RootConfig.class}*/)
public abstract class BaseTestNG extends AbstractTestNGSpringContextTests {

	protected final MockHttpServletRequest request = new MockHttpServletRequest();
	
	protected final MockHttpServletResponse response = new MockHttpServletResponse();
	
	protected final MockHttpSession session = new MockHttpSession();
	
	protected final ObjectMapper om = new ObjectMapper();
	
	
	@BeforeTest
	public void setActionContext() {
		request.setSession(session);
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
	}
	
	public String println(Object obj) {
		Assert.assertNotNull(obj);
		String result = null;
		try {
			result = om.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		System.out.println(result);
		return result;
	}
	
}
